/**
 * FileName: RoleAccessDaoImpl.java
 * 角色权限Dao实现类
 *
 * @author  王威
 * @Date    2021/11/29
 * @version 1.00
 **/
package com.site.dao.impl;
import com.site.dao.RoleAccessDao;
import com.site.pojo.Employee;
import com.site.pojo.RoleAccess;
import com.site.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleAccessDaoImpl implements RoleAccessDao {
    /*查询所有角色权限*/
    @Override
    public List<RoleAccess> selectAll() throws SQLException {
        List<RoleAccess> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        ResultSet rst = JDBCUtils.executeQuery(conn, "SELECT * FROM `role_access`");
        while (rst.next()){
            RoleAccess roleAccess = new RoleAccess();
            roleAccess.setAid(rst.getLong("aid"));
            roleAccess.setRid(rst.getLong("rid"));
            list.add(roleAccess);
        }
        JDBCUtils.close(rst,null,conn);
        return list;
    }

    /*按照id查询角色权限*/
    @Override
    public RoleAccess selectById(RoleAccess roleAccess) throws SQLException {
        RoleAccess roleAccess1 = null;
        Connection conn = JDBCUtils.getConnection();
        ResultSet rst = JDBCUtils.executeQuery(conn, "SELECT * FROM `role_access` where rid = ?",roleAccess.getRid());
        while (rst.next()){
            roleAccess1 = new RoleAccess();
            roleAccess1.setAid(rst.getLong("aid"));
            roleAccess1.setRid(rst.getLong("rid"));
        }
        JDBCUtils.close(rst,null,conn);
        return roleAccess1;
    }

    /*添加角色权限*/
    @Override
    public int addRoleAccess(Connection con, RoleAccess roleAccess) throws SQLException {
        String sql = "insert into ` hurricane_house `.` role_access ` (` aid `, `rid`)values(?,?);";
        Object[] prmas = {roleAccess.getAid(),roleAccess.getRid()};
        return JDBCUtils.executeUpdate(con,sql,prmas);
    }
    /*删除角色权限数据*/
    @Override
    public int delRoleAccess(Connection con, RoleAccess roleAccess) {
        String sql = "delete from ` ` role_access ` where aid  =?";
        return JDBCUtils.executeUpdate(con,sql,new Object[]{roleAccess.getAid()});
    }
    /*修改角色权限数据*/
    @Override
    public int updateRoleAccess(Connection con, RoleAccess roleAccess) {
        String sql = "update ` hurricane_house `.` role_access ` set ` rid ` = ? where ` aid ` = ?";
        Object[] prmas = {roleAccess.getRid(),roleAccess.getAid()};
        return JDBCUtils.executeUpdate(con,sql,prmas);
    }
}
